<template>
	<view class="gb-content">
		<view class="center-box">
			<image :src="logo"></image>
			<view class="box-text-1">Hi，请登录~</view>
			<view class="box-text-2">为保证系统的正常运行请授权登录~</view>
			<button size="default" class="box-btn-1" type="warning"
			open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权登录</button>
			<!-- <button @click="getUserInfo" withCredentials="true" class="box-btn-1">授权登录</button> -->
			<navigator open-type="switchTab" url="/pages/shop/index/index" class="box-btn-2" >暂不授权</navigator>
			<!-- #ifdef MP-WEIXIN -->
			<!-- #endif -->
			<editUserModal :isShow="editModal" :userId="user_id" @closeEdit="closeEdit" @editSuccess="editSuccess">
			</editUserModal>
		</view>
		
	</view>
</template>

<script>
	import editUserModal from '@/components/eidtUserModal/index.vue'
	var settings = uni.getStorageSync('setting');
	export default{
		components: {
			editUserModal
		},
		data() {
			return {
				logo: this.config.baseUrl + settings.logo,
				returnUrl:'',
				bindMobile:false,
				userInfo:{
					avatarUrl:'/static/public/images/headimgurl.jpg',
					name:''
				},
				options:{},
				editModal:false,
				loginData:{},
				user_id:0
			}
		},
		onLoad(options) {
			this.options = options
			this.returnUrl = options.returnUrl;
			this.app.getWxOauthOpenId(this,this.options);
		},
		methods: {
			async getPhoneNumber (e) {
				let _this = this
				//小程序手机授权登陆
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let share_token = uni.getStorageSync("share_token");//获取邀请码
				    this.$u.post('weixin/api.index/loginByMpPhone', {
						iv:e.detail.iv,
						encryptedData:e.detail.encryptedData,
						share_token:share_token,
						session_key:uni.getStorageSync("session_key"),
						openid:this.app.getWxOpenId()
					}).then(res => {
						_this.loginData = res.data
						if(!res.data.nickname || res.data.nickname == '微信用户'){
							_this.user_id = res.data.user_id
							_this.editModal = true
						}else{
							//登录成功后执行
							_this.app.setAuthCode(res.data.token);
							if (typeof(_this.returnUrl) == 'undefined') { //未读取到页面信息
								uni.redirectTo({
									url: '/pages/member/center/index'
								});
								return false;
							}else{
								uni.redirectTo({
									url:'/'+decodeURIComponent(_this.returnUrl),
								});
							}
						}
					})
				}
			},
			loginGo(){
				//登录成功后执行
				if (typeof(this.returnUrl) == 'undefined') { //未读取到页面信息
					uni.redirectTo({
					    url: '/pages/member/center/index'
					});
					return false;
				}
				uni.redirectTo({
					url:'/'+decodeURIComponent(this.returnUrl),
				});
			},
			editSuccess() {
				let _this = this
				_this.editModal = false
				//登录成功后执行
				uni.setStorageSync("share_token",_this.loginData.user_token);
				_this.app.setAuthCode(_this.loginData.token);
				if (typeof(_this.returnUrl) == 'undefined') { //未读取到页面信息
					_this.navTo('/pages/member/center/index')
					return false;
				}else{
					_this.navTo('/'+decodeURIComponent(_this.returnUrl))
				}
			},
			closeEdit() {
				this.editModal = false
			},
			async getUserInfo(){
				var that =this;
				let _this = this;
				// #ifdef H5
				this.app.getWxOauthOpenId(this,this.options);
				// #endif
				// #ifdef MP-WEIXIN
				let share_token = uni.getStorageSync("share_token");//获取邀请码
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						_this.$u.post(`weixin/api.index/getOpenId?type=MP&code=${loginRes.code}&share_token=${share_token}&openid=`+that.app.getWxOpenId())
						.then(res => {
							_this.loginData = res.data
							_this.app.setWxOpenId(res.data.openid);
							if(!res.data.nickname || res.data.nickname == '微信用户'){
								_this.user_id = res.data.user_id
								_this.editModal = true
							}else{
								//登录成功后执行
								uni.setStorageSync("share_token",res.data.user_token);
								_this.app.setAuthCode(res.data.token);
								if (typeof(_this.returnUrl) == 'undefined') { //未读取到页面信息
									uni.redirectTo({
										url: '/pages/member/center/index'
									});
									return false;
								}else{
									uni.redirectTo({
										url:'/'+decodeURIComponent(_this.returnUrl),
									});
								}
							}
						});
					},
					fail: function(err) {
						console.log(err)
					}
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	.gb-content{
		background-color: #fff !important;
	}
	.center-box{
		display: flex;
		flex-direction: column;
		align-items: center;
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		bottom: 0;
		image{
			width: 200upx;
			height: 200upx;
			margin-top: 143upx;
			border: 1px solid #e3e3e3;
			border-radius: 50%;
		}
		.box-text-1{
			margin-top: 66upx;
			font-family: AdobeHeitiStd-Regular;
			font-size: 40rpx;
			font-weight: bold;
			font-stretch: normal;
			color: #333333;
		}
		.box-text-2{
			margin-top: 29upx;
			font-size: 27upx;
			color: #333333;
			
		}
		.box-btn-1{
			margin-top: 81upx;
			font-size: 35upx;
			width: 450upx;
			height: 75upx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #ff5428;
			border-radius: 35upx;
			color: #fff;
		}
		
		.box-btn-2{
			margin-top: 50upx;
			font-size: 30upx;
			text-decoration: underline;
		}
		
	}

</style>
